package cc.chenwenxi.mypi.appium.listener;

import cc.chenwenxi.mypi.appium.kits.AppiumDriverWait;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.events.api.general.AppiumWebDriverEventListener;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

@Slf4j
public class AppiumAllListener implements AppiumWebDriverEventListener {

    private final int timeOut = 30;

    @Override
    public void beforeFindBy(By by, WebElement webElement, WebDriver webDriver) {
        String locator = null;
        try {
//            locator = splitBy(by);
            locator = by.toString();
        } catch (Exception e) {
            log.error("by不能按格式切割！");
        }
        log.info("beforeFindBy监听.寻找:{} ing...",locator);
        try {
            AppiumDriver appiumDriver = (AppiumDriver) webDriver;
            AndroidElement androidElement = (AndroidElement) webElement;
            new AppiumDriverWait(appiumDriver, timeOut).untilMy(appiumDriver, androidElement).isDisplayed();
        } catch (Exception e) {
            log.warn("beforeFindBy监听{}秒,不可见:{}",timeOut,locator);
        }
    }

    @Override
    public void beforeClickOn(WebElement webElement, WebDriver webDriver) {
        try {
            log.info("beforeClickOn:Clicking............"
                    + splitElement(webElement));
        } catch (Exception e) {
            log.error("element不能按格式切割！");
        }
    }

    @Override
    public void beforeChangeValueOf(WebElement webElement, WebDriver webDriver) {

    }

    @Override
    public void afterChangeValueOf(WebElement webElement, WebDriver webDriver) {

    }

    @Override
    public void beforeAlertAccept(WebDriver webDriver) {

    }

    @Override
    public void afterAlertAccept(WebDriver webDriver) {

    }

    @Override
    public void afterAlertDismiss(WebDriver webDriver) {

    }

    @Override
    public void beforeAlertDismiss(WebDriver webDriver) {

    }

    @Override
    public void beforeNavigateTo(String s, WebDriver webDriver) {

    }

    @Override
    public void afterNavigateTo(String s, WebDriver webDriver) {

    }

    @Override
    public void beforeNavigateBack(WebDriver webDriver) {

    }

    @Override
    public void afterNavigateBack(WebDriver webDriver) {

    }

    @Override
    public void beforeNavigateForward(WebDriver webDriver) {

    }

    @Override
    public void afterNavigateForward(WebDriver webDriver) {

    }

    @Override
    public void beforeNavigateRefresh(WebDriver webDriver) {

    }

    @Override
    public void afterNavigateRefresh(WebDriver webDriver) {

    }



    @Override
    public void afterFindBy(By by, WebElement webElement, WebDriver webDriver) {

    }



    @Override
    public void afterClickOn(WebElement webElement, WebDriver webDriver) {

    }

    @Override
    public void beforeChangeValueOf(WebElement webElement, WebDriver webDriver, CharSequence[] charSequences) {

    }

    @Override
    public void afterChangeValueOf(WebElement webElement, WebDriver webDriver, CharSequence[] charSequences) {

    }

    @Override
    public void beforeScript(String s, WebDriver webDriver) {

    }

    @Override
    public void afterScript(String s, WebDriver webDriver) {

    }

    @Override
    public void beforeSwitchToWindow(String s, WebDriver webDriver) {

    }

    @Override
    public void afterSwitchToWindow(String s, WebDriver webDriver) {

    }

    @Override
    public void onException(Throwable throwable, WebDriver webDriver) {

    }

    @Override
    public <X> void beforeGetScreenshotAs(OutputType<X> outputType) {

    }

    @Override
    public <X> void afterGetScreenshotAs(OutputType<X> outputType, X x) {

    }

    @Override
    public void beforeGetText(WebElement webElement, WebDriver webDriver) {

    }

    @Override
    public void afterGetText(WebElement webElement, WebDriver webDriver, String s) {

    }

    private String splitElement(WebElement element) {
        String str = element.toString().split("-> ")[1];
        return str.substring(0, str.length() - 1);
    }

    private String splitBy(By by) {
        String str = by.toString().split("-")[1].toString().split(":")[3];
        return str.substring(0, str.length() - 3);
    }
}
